﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIItemTransformUnitDrawVoxelComponent : UIItemTransformUnitComponent
{
    public TransformUnitComponent boneUnit;

    /*
    public override void LookThis()
    {
        if (lookNow && lookNow is UIItemTransformUnitDrawVoxelComponent)
        {
            ((UIItemTransformUnitDrawVoxelComponent)lookNow).spriteBG.SetActive(false);
        }
        spriteBG.SetActive(true);
        lookNow = this;
        UIComponentMastermind.instance.ShowLeftUITransform();
    }
    public override void UILookThis()
    {
        TransformVector.Thas.Look = unit;
        if (lookNow && lookNow is UIItemTransformUnitDrawVoxelComponent)
        {
            ((UIItemTransformUnitDrawVoxelComponent)lookNow).spriteBG.SetActive(false);
        }
        spriteBG.SetActive(true);
        lookNow = this;
        UIComponentMastermind.instance.ShowLeftUITransform();
    }

    public override void NoLookThis()
    {
        if (lookNow && lookNow is UIItemTransformUnitDrawVoxelComponent)
        {
            ((UIItemTransformUnitDrawVoxelComponent)lookNow).spriteBG.SetActive(false);
            lookNow = null;
        }
    }
    */
    public void CheckBox()
    {
        TransformVector.Thas.Look = unit;
    }

    //private IEnumerator SetName()
    //{
    //    // yield return new WaitForSeconds(0.2f);
    //    yield return new WaitForEndOfFrame();
    //    inputName.value = _name;
    //}

    public void SetZero()
    {
        if (unit.gameObject.GetComponent<TransformUnitComponent>())
        {
           
            Position = unit.gameObject.GetComponent<TransformUnitComponent>().zeroPosition;
        }
        else
        {
            Position = new Vector3(0, 0, 0);
        }
    }


    public void EsitorChangeCheckbox()
    {
        TransformVector.Thas.Look = unit;
        EsitorChange();
    }

    public void EsitorChange()
    {
        unit.gameObject.GetComponent<DrawVoxel3DCoreComponent>().EsitorChange();
    }

    public void Merge()
    {
        if (unit.gameObject.GetComponent<DrawVoxel3DCoreComponent>().Merge())
        {
            Delete();
        }
    }

    public override void Delete()
    {
        if (boneUnit)
        {
            unit.gameObject.GetComponent<DrawVoxel3DCoreComponent>().Delete();
            boneUnit.UiComponent.Delete();
        }
        base.Delete();
    }


    public void CreateBone()
    {
        if (boneUnit)
        {
            TransformVector.Thas.Look = boneUnit.transform;
        }
        else
        {
            Transform boneUnitTransform = Instantiate(UINodeTransformComponent.thas.itemTransformUnitBoneGameObjectPrefab).transform;
            boneUnitTransform.gameObject.SetActive(true);
            boneUnitTransform.position = Position;
            boneUnitTransform.name = Name;
            boneUnitTransform.parent = UINodeTransformComponent.thas.itemTransformUnitBoneGameObjectPrefab.transform.parent;//temp


            UIItemTransformUnitComponent ui;
            boneUnit = UINodeTransformComponent.thas.AddItem
                (Name + "_Bone", Position, new Vector3(0, 0, 0), boneUnitTransform, typeof(UIItemTransformUnitBoneComponent),out ui);
            TransformVector.Thas.Look = boneUnit.transform;
            unit.GetComponent<UnitMeshPointComponent>().m_boneTarget = boneUnit.transform;
            //UnitMeshPointComponent
        }
    }

    public void CreateCustomBone(Vector3 worldPosition)
    {
        if (boneUnit)
        {
            boneUnit.UiComponent.unit.localPosition = worldPosition;
        }
        else
        {
            Transform boneUnitTransform = Instantiate(UINodeTransformComponent.thas.itemTransformUnitBoneGameObjectPrefab).transform;
            boneUnitTransform.name = Name;
            boneUnitTransform.parent = UINodeTransformComponent.thas.itemTransformUnitBoneGameObjectPrefab.transform.parent;//temp
            boneUnitTransform.localPosition = worldPosition;

            UIItemTransformUnitComponent ui;
            boneUnit = UINodeTransformComponent.thas.AddItem
                (Name + "_Bone", worldPosition, new Vector3(0, 0, 0), boneUnitTransform, typeof(UIItemTransformUnitBoneComponent), out ui);
            //TransformVector.Thas.Look = boneUnit.transform;
            unit.GetComponent<UnitMeshPointComponent>().m_boneTarget = boneUnit.transform;
            //UnitMeshPointComponent
        }
    }
}
